import { HealthCheckTask } from "./health.check.class";
import { PanelGroupKey, V3proSubjectMessage } from "../model/v3pro.model";
import { V3proScreenAllStatus } from "../../../api/v3pro-api/v3pro.api.response.model";
import { V3proScreenLayoutHelper } from "../helper/v3pro.screen.layout.helper";
import dayjs from "dayjs";
import { appConfig } from "../../../configs/env.config.load";
import {
  v3proScreenAllStatusApi,
  v3proScreenApi,
  v3proScreenStartDeadPixelCheck,
} from "../../../api/v3pro-api/v3pro.screen.api";
import { loggerStdout } from "../../../configs/log.config";
import { V3proAllStatusLambda } from "../helper/v3pro.all.status.lambda.helper";
import { cloneDeep } from "lodash";

/**
 * 显示坏点故障
 *
 * 是否支持阈值设置： Y
 * "[0%,100%]
 * 默认20%"	"定时查询
 *
 * 查询周期:
 * 间隔24小时
 *
 * 告警产生的原因:
 * 告警产生的原因 检测到显示像素灯珠开路，单个面板坏点百分比超出阈值
 *
 * 异常现象:
 * 面板中有显示像素不能点亮
 *
 * 维修建议:
 * 更换面板
 *
 * 告警级别: 重要
 */

let lastDay = dayjs().day() - 1;
export class V3proCheckDeadPixelTriggerTask extends HealthCheckTask {
  async __check() {
    const today = dayjs();
    const triggerTime = dayjs(appConfig.v3pro.collectTimer.deadPixel)
      .set("year", today.year())
      .set("month", today.month())
      .set("date", today.date());
    if (triggerTime.isBefore(dayjs()) && today.day() != lastDay) {
      loggerStdout.info("dead pixel trigger");
      const screens = await v3proScreenApi(this.__addCancelToken());
      const scanBoards = [];
      for (const scr of screens.requestDatas) {
        const scrInfo = await v3proScreenAllStatusApi(scr.id, this.__addCancelToken());
        const sbList = V3proAllStatusLambda.filterScanBoardsWithRemove(scrInfo, () => true);
        scanBoards.push(...cloneDeep(sbList[0]).map((sb) => ({ id: sb.deviceInfo.id })));
      }
      loggerStdout.info("scanBoards: %s", JSON.stringify(scanBoards));
      await v3proScreenStartDeadPixelCheck(scanBoards, this.__addCancelToken());
      lastDay = today.day();
    }
  }

  async checkResult(
    keyParams: PanelGroupKey[],
    screenId: string,
    allStatus: V3proScreenAllStatus,
    screenLayoutHelper: V3proScreenLayoutHelper,
  ): Promise<V3proSubjectMessage> {
    return new V3proSubjectMessage();
  }

  getInterval(): number {
    return 10 * 1000;
  }
}
